package com.googlecode.nls4j.benchmarks.tl;

import java.util.EnumSet;
import java.util.Locale;

import com.googlecode.nls4j.CurrentBundle;
import com.googlecode.nls4j.EnumBundleLoader;
import com.googlecode.nls4j.KeyValuePair;
import com.googlecode.nls4j.KeyValuePairStorage;
import com.googlecode.nls4j.ThreadLocalBundle;

public enum KvpMsg implements KeyValuePair<String> {
    M1("M1 default value"),
    M2("M2 default value"),
    M3("M3 default value");

    private static final ThreadLocalBundle<KvpMsg, String> localBundle = ThreadLocalBundle
            .newInstance(
                new KeyValuePairStorage<KvpMsg, String>(
                        KvpMsg.class, EnumBundleLoader
                                .<KvpMsg, String> getDefault(false), EnumSet
                                .allOf(KvpMsg.class)), false);

    private final String defaultValue;
    private final ThreadLocal<String> localValue = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {
            return defaultValue;
        }
    };

    private KvpMsg(final String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public static CurrentBundle<KvpMsg, String> getCurrentBundle() {
        return localBundle;
    }

    @Override
    public String getName() {
        return name();
    }

    @Override
    public String getDefaultValue(final Locale locale) {
        return defaultValue;
    }

    @Override
    public String getValue() {
        return localValue.get();
    }

    @Override
    public void setValue(final String value) {
        localValue.set(value);
    }

    @Override
    public String toString() {
        return localValue.get();
    }
}
